package org.enwey.icarenew.Activity.Login;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.http.SslError;
import android.os.Bundle;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import org.enwey.icarenew.APPUtils.ToastUtil;
import org.enwey.icarenew.Activity.Base.BaseActivity;
import org.enwey.icarenew.R;


/**
 * Created by enwey on 2014/11/23.
 */
public class UseProtocolActivity extends BaseActivity {
    private TextView mAgree;
    private WebView mProtocol;
    private final String PROTOCOL_PATH = "http://icarenew.sinaapp.com";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_protocol);
        setUp();
        findView();
        registerListener();
    }

    private void setUp() {
        showLeftBtn();
        setTitle("用户协议");
    }

    private void findView() {
        mAgree = (TextView) this.findViewById(R.id.activity_protocol_btn);
        mAgree.setVisibility(View.GONE);
        mProtocol = (WebView) this.findViewById(R.id.activity_protocol_web);
        setProtocolToWeb();
    }

    private void setProtocolToWeb() {
        showLoading();
        WebSettings settings = mProtocol.getSettings();
        mProtocol.loadUrl(PROTOCOL_PATH);
        settings.setJavaScriptEnabled(true);
        settings.setSupportZoom(false);
        settings.setBuiltInZoomControls(true);
        mProtocol.setBackgroundColor(0);
        Drawable background = mProtocol.getBackground();

        if (background != null) {
            background.setAlpha(0);
        }


        mProtocol.setWebViewClient(new MyWebViewClient());
    }

    private void registerListener() {
        View.OnClickListener register = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                agreeProtocol();
            }
        };
        mAgree.setOnClickListener(register);
    }

    private void agreeProtocol() {
        Intent intent = new Intent(UseProtocolActivity.this, RegisterPhoneNumber.class);
        intent.putExtra("agree", true);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        ToastUtil.show(UseProtocolActivity.this, "我是同意按钮，点跳转到注册手机号码界面...");
        setResult(20,intent);
        finish();
    }

    public class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return super.shouldOverrideUrlLoading(view, url);
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            showLoading();
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            hideLoading();
            mAgree.setVisibility(View.VISIBLE);
        }

        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            super.onReceivedError(view, errorCode, description, failingUrl);
            hideLoading();
            if (errorCode != 200) {
                mAgree.setVisibility(View.GONE);
                ToastUtil.show(UseProtocolActivity.this, "网络异常，请检查网路!");
                finish();
                return;
            }
        }
    }
}
